<?php

	
/*************************************************strat类目属性API*****************************************/
	/**
	 * taobao.itempropvalues.get:获取标准类目属性值    [查]
	 **/
	function Top_ItempropvaluesGet($userParamArr){
		$client = new Client();  
	    setUserParameter($client,$userParamArr);   
	    return returnCallBackPage($client,itempropvaluesGet,'prop_value'); 
	}
	/**
	 * taobao.itemcats.authorize.get :查询B商家被授权品牌列表和类目列表   [商]
	 **/
	function Top_ItemcatsAuthorizeGet( $userParamArr ){
		$client = new Client();  
	    setUserParameter($client,$userParamArr);  
	    return returnCallBackPage($client,itemcatsAuthorizeGet); 
	}
	/**
	 * taobao.itemcats.get:获取后台供卖家发布商品的标准商品类目  [查]
	 **/
	function Top_ItemcatsGet($userParamArr){
		$client = new Client();  
	    setUserParameter($client,$userParamArr);  
	    return returnCallBackPage($client,itemcatsGet); 
	}
	/**
	 * taobao.itemprops.get :获取标准商品类目属性     [查]
	 **/
	function Top_ItempropsGet($userParamArr){
		$client = new Client();  
	    setUserParameter($client,$userParamArr);  
	    return returnCallBackPage($client,itempropsGet,'item_prop'); 
	}
	/**
	 * taobao.itemprops.get.v2 :获取标准商品类目属性     [查]
	 **/
	function Top_itempropsGetV2($userParamArr){
		$client = new Client();  
	    setUserParameter($client,$userParamArr);  
	    return returnCallBackPage($client,itempropsGetV2); 
	} 
/*************************************************end 类目属性API*****************************************/
	 

	/**
	 * 属性的pid调用taobao.itemprops.get.v2取得 
	 * */
	function GetItemPropsByCid($cid){  
		global $exc,$db,$os,$topParamArr;   
		$userParamArr = array(
			'fields' => 'pid,name,is_key_prop,is_sale_prop,is_color_prop,is_enum_prop,is_input_prop,is_item_prop,child_template,must,multi,
parent_pid,parent_vid,status,sort_order',
			 'cid' =>$cid 
		);    
		$result = Top_itempropsGetV2( $userParamArr ); // taobao.itemprops.get.v2 :获取标准商品类目属性
		$pvs='';
		if(is_array($result['item_prop'])) {  
				    foreach($result['item_prop'] AS $item_cat){   
						  OS_InsertIntoTB('tb_itemprop','pid', $item_cat);  //table('tb_itemprop'), $db, 'id', 'pid');   
						  if ( $pvs !='' ) {
							  	 $pvs = $pvs  .';' . $item_cat['pid']; 
							  }else {
							  	  $pvs = $pvs  . $item_cat['pid']; 
							  } 
  						 }  
					  $userParamArr = array(
									'fields' => 'cid,pid,prop_name,vid,name,name_alias,status,sort_order',
								    'cid' =>$cid ,
								    'pvs' => $pvs
								);   
					  $item_cats =Top_ItempropvaluesGet( $userParamArr );   //taobao.itempropvalues.get:获取标准类目属性值
					  InsertIntoTB_PropValue( $item_cats['list'], $cid  );     // 插入表 PropValue  taobao 属性表 PropValue  	 
			 }  

		//下载完成后，更改类目的field1为1，表示已经下载过类目属性  
		$sql = "select cid from ".$os->table('tb_itemcat')." where cid='".$cid."'";                   
		if ( $db->getOne($sql) ){  
			$db->query("update  ".$os->table('tb_itemcat')." set field1=1 where cid='".$cid."'") ;
		}  
	} 
	/**
	 * 插入表TB_PropValue 对应taobaao 商品属性值(PropValue) 
	 * */
	function InsertIntoTB_PropValue( $prop_values, $cid ){
		 global  $db,$os,$topParamArr;  
		 $itemcat = $db->getRow("select * from open_tb_itemcat where cid='$cid'"); 
	     if( is_array( $prop_values ) ){
		    	foreach ($prop_values as $prop_value){   
					OS_InsertIntoTB('tb_propvalue','vid', $prop_value); 	   
				    /**数据插入guige1 与 guige2 **/
				 	if ( strstr(addslashes( $prop_value['prop_name'] ),"颜色")) {  
				 		 $prop_value['is_parent'] = $itemcat['is_parent'];   
					     OS_InsertIntoTB('tb_guige1','vid', $prop_value, "cid='".$cid."'" ) ;  	
				 	    } 
	 			    if ( strstr(addslashes( $prop_value['prop_name'] ),"尺码") || strstr(addslashes( $prop_value['prop_name'] ),"尺寸") ) {
	 			    	  $prop_value['is_parent'] = $itemcat['is_parent']; 
					      OS_InsertIntoTB('tb_guige2','vid',$prop_value, "cid='".$cid."'") ;   
	 			       }/**数据插入guige1 与 guige2 **/
			     }  
	      } 
	}  
	/**
	 * 递归下载淘宝类目ItemCat   
	 */
    function AddItemCat($cid) {   //对应taobao数据库 ItemCat:商品类目   ItemCat 表 
    	global  $db,$os ;  
    		 if(! Check_is_exist('tb_itemcat','cid',$cid,'','cid') ){   
    		 	   $userParamArr = array( 
					     			'fields'   =>'cid,parent_cid,name,is_parent,status,sort_order',
									'cids'=>$cid 
					      ); 
			       $result =Top_ItemcatsGet( $userParamArr );
			       OS_InsertIntoTB('tb_itemcat','cid',$result['item_cat']); 
    		 } 
		     $userParamArr = array( 
			     			'fields'   =>'cid,parent_cid,name,is_parent,status,sort_order',
							'parent_cid'=>$cid 
			      );  
			 $result =Top_ItemcatsGet( $userParamArr );  
			 if(is_array($result['item_cat'])) { 
				    foreach($result['item_cat'] AS $item_cat) { 
				    	 OS_InsertIntoTB('tb_itemcat','cid',$item_cat);  
					     AddItemCat($item_cat['cid'] ) ;   //递归下载类目
				    }
				} 
		   //成功下载类目后 下载淘宝类目属性$os->table('tb_itemcat')
    	   $sql = "select cid from ".$os->table('tb_itemcat')."  where parent_cid >0 and (field1<>1 or field1 is null)";  
 		    if (is_array($rows=$db->getAll($sql))){ 
         	  foreach($rows  AS $item_cat){    
		     	  GetItemPropsByCid( $item_cat['cid']  );// GetTb_ItemPropList(  );//   echo ($item_cat['lmdm'] );
		        }
	        }  
	 }  
?>